package redteam.grabthegreen;

import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.Contacts.People;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;

public class ContactListWindow extends ListActivity implements OnItemClickListener{

	 private static String[] PROJECTION = new String[] {
	        People._ID, People.NAME, People.PREFERRED_EMAIL_ID
	    };

	    @Override
	    protected void onCreate(Bundle icicle) {
	        super.onCreate(icicle);
	        setContentView(R.layout.contactlistwindow);
	        mPhone = (TextView) findViewById(R.id.email);
	        getListView().setOnItemClickListener(this);


	        // Get a cursor with all people
	        Cursor c = getContentResolver().query(People.CONTENT_URI, PROJECTION, null, null, null);
	        startManagingCursor(c);
	        mPhoneColumnIndex = c.getColumnIndex(People.NAME);


	        ListAdapter adapter = new SimpleCursorAdapter(this,
	                android.R.layout.simple_list_item_1, // Use a template
	                                                        // that displays a
	                                                        // text view
	                c, // Give the cursor to the list adapter
	                new String[] {People.NAME}, // Map the NAME column in the
	                                            // people database to...
	                new int[] {android.R.id.text1}); // The "text1" view defined in
	                                            // the XML template
	        setListAdapter(adapter);
	    }

	    public void onItemClick(AdapterView parent, View v, int position, long id) {
	        if (position >= 0) {
	            Cursor c = (Cursor) parent.obtainItem(position);
	            mPhone.setText(c.getString(mPhoneColumnIndex));
	            em = c.getString(mPhoneColumnIndex);
	            setResult(RESULT_OK,em);
	            finish();
	        }
	    }



	    private int mPhoneColumnIndex;
	    private TextView mPhone;
	    private String em;
} // end class
